//
//  MKNewFeatureController.swift
//  Weibo
//
//  Created by mac on 15/12/16.
//  Copyright © 2015年 MK. All rights reserved.
//

import UIKit
import SnapKit

//2.创建重用标识
private let reuseIdentifier = "CCell"
private let cellCount = 4

class MKNewFeatureController: UICollectionViewController {
    
    //把流水布局写在初始化方法中 －想用CollectionView就必须指定布局
    init() {
        
        //1.初始化布局
        let flow = UICollectionViewFlowLayout()
        
        flow.itemSize = UIScreen.mainScreen().bounds.size
        flow.scrollDirection = .Horizontal
        flow.minimumInteritemSpacing = 0
        flow.minimumLineSpacing = 0
        
        super.init(collectionViewLayout: flow)
        
        //下面的都是继承自scrollView的属性 所以需要先初始化父类
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //3.注册重用标识
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    
    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cellCount
    }
    
    //4.获取cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        //cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.purpleColor() : UIColor.lightGrayColor()
        
        cell.index = indexPath.item
        //print(indexPath.item)
        
        return cell
    }
    
    //代理方法 - 展示完成cell后调用  做按钮的动画效果
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //由于这个indexPath.item跟实际的不一样 比较操蛋 所以要先纠正下标索引
        //print(indexPath.item)
        //获取正确的cell
        let cell = collectionView.visibleCells().last as! NewFeatureCell
        //获取正确的indexPath
        let indexPath = collectionView.indexPathForCell(cell)!
        //print(indexPath.item)
        //判断新特性的按钮 是否 执行动画
        if indexPath.item == cellCount - 1 {
            cell.btnDidAnimation()
        }
        
    }
    
}

//同一个文件中是可以创建多个类的 - 自定义cell
class NewFeatureCell: UICollectionViewCell {
    
    var index: Int = 0 {
        //重写Set方法
        didSet {
            backImageView.image = UIImage(named: "new_feature_\(index + 1)")
            //enterBtn.hidden = index != 3 ? true : false
            enterBtn.hidden = true
        }
    }
    
    //重载构造方法 - 设置item的尺寸大小
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        showViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //按钮的动画效果
    func btnDidAnimation() {
        //通过修改按钮的缩放比例 来展示动画效果 - 按钮的原大小
        enterBtn.transform = CGAffineTransformMakeScale(0, 0)
        enterBtn.hidden = false
        /*
        usingSpringWithDamping: 0.98 弹簧系数 越小越弹
        initialSpringVelocity: 9.8   重力加速度
        options:                     动画的可选项  swift中使用数组
        */
        
        UIView.animateWithDuration(0.25, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //恢复按钮的原始大小
            self.enterBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                //print("新特性按钮 - 动画执行完后的回调")
        }    }
    
    func showViews() {
        //SnapKit框架 添加子控件 并进行自动布局
        contentView.addSubview(backImageView)
        contentView.addSubview(enterBtn)
        backImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
        enterBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).inset(188)
        }
        enterBtn.addTarget(self, action: "clickEnterBtn", forControlEvents: .TouchUpInside)
    }
    
    @objc private func clickEnterBtn() {
        print("新特性的按钮点击事件")
        //FIXME: -利用通知 更改根控制器跳转到到首页
        NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil)
    }
    
    //懒加载所有用到的子视图
    lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    lazy var enterBtn: UIButton = UIButton(imageName: "new_feature_finish_button", titleColor: UIColor.whiteColor(), titleName: "欢迎体验", titleFont: 14)

    
}

